home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK2.toast / Development Kits (Disc 2) / QuickTime / Programming Stuff / Sample Code / Music Architecture / Mixed Bag / •QTMusic Sample Sequencer / SequencerTest Filing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-05  |  4.1 KB  |  247 lines  |  [TEXT/KAHL]

  1. /*
  2.  * file: SequencerTest Filing.c
  3.  *
  4.  * started 12 January 1992 09:34
  5.  * david van brink
  6.  *
  7.  * The filing routines
  8.  *
  9.  */
  10.  
  11.  
  12. /*--------------------------
  13.     Inclusions
  14. --------------------------*/
  15.  
  16. #include <Files.h>
  17. #include <StandardFile.h>
  18. #include <Resources.h>
  19. #include <Errors.h>
  20. #include <Memory.h>
  21.  
  22. #include "BigEasy2.h"
  23. #include "BigEasyDialogs.h"
  24.  
  25. #include "SequencerTest.h"
  26. #include "SequencerTest Filing.h"
  27.  
  28. /*--------------------------
  29.     Local Prototypes
  30. --------------------------*/
  31.  
  32. OSErr InternalSaveData(TDoc *d);
  33. void UpdateSaveRecordFromEarlierVersion(TSaveRecord **sr);
  34.  
  35. /*--------------------------
  36.     Wasabe
  37. --------------------------*/
  38.  
  39. void OpenDoc(short n,short item, short ref)
  40.     {
  41.     StandardFileReply sfr;
  42.     OSType ourType[5];
  43.     ourType[0] = kDocumentFileType;
  44.     StandardGetFile(nil,1,ourType,&sfr);
  45.  
  46.     if(sfr.sfGood)
  47.         {
  48.         OpenDocSpec(&sfr.sfFile);
  49.         }
  50.     }
  51.  
  52.  
  53. void OpenDocSpec(FSSpec *fSpec)
  54.     {
  55.     short i;
  56.     short refNum;
  57.     register TDoc *d;
  58.     TSaveRecord **sr;
  59.     FInfo fileInfo;
  60.  
  61.     for(i = 0; i<kDocMax; i++)
  62.         {
  63.         d = &gDoc[i];
  64.         if(!d->used)
  65.             goto gotOne;
  66.         }
  67.  
  68.     EasyDialogMessage(0,(StringPtr)0x910,"\pNo more windows may be opened.",
  69.             kEasyDialogOkay);
  70.     goto goHome;
  71.  
  72. gotOne:
  73.     refNum = FSpOpenResFile(fSpec,fsRdPerm);
  74.     if(refNum == 1)
  75.         goto fileError;
  76.     sr = (void *)Get1IndResource(kDocumentResType,1);
  77.     if(!sr)
  78.         goto closeAndFileError;
  79.     HLock((void *)sr);
  80.     if((**sr).docVersion != kDocVersion)
  81.         {
  82.         EasyDialogMessage(0,fSpec->name,
  83.                 "\pThat was an old version of the file format.",
  84.                 kEasyDialogOkay);
  85.         goto closeAndFileError;
  86.         }
  87.  
  88.  
  89.     UpdateSaveRecordFromEarlierVersion(sr);
  90.  
  91.  
  92.     d->used = true;
  93.     d->docSpec = *fSpec;
  94.     NewDocFromSaveRecord(i,&(**sr));
  95.  
  96.     goto goHome;
  97.  
  98. closeAndFileError:
  99.     CloseResFile(refNum);
  100. fileError:
  101.     EasyDialogMessage(0,fSpec->name,
  102.             "\pThere was a problem opening the file.",
  103.             kEasyDialogOkay);
  104.  
  105. goHome:;
  106.     }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. short SaveDoc(short n,short item, short ref)    
  113. /*
  114.  * return nonzero to
  115.  * say it was cancelled.
  116.  */
  117.     {
  118.     TDoc *d;
  119.  
  120.     d = &gDoc[n - kFirstDocWindow];
  121.  
  122.     if(!d->everSaved)
  123.         return SaveAsDoc(n,item,ref);
  124.     else
  125.         return InternalSaveData(d);
  126.     }
  127.  
  128. short SaveAsDoc(short n,short item, short ref)
  129.     {
  130.     StandardFileReply sfr;
  131.     register TDoc *d;
  132.     Boolean result;
  133.     TSaveRecord *sr,**srH;
  134.     short refNum;
  135.     OSErr thisError;
  136.     OSType fileType;
  137.  
  138.     d = &gDoc[n - kFirstDocWindow];
  139.  
  140.     StandardPutFile("\p",d->docSpec.name,&sfr);
  141.  
  142.     result = !sfr.sfGood;
  143.     if(sfr.sfGood)
  144.         {
  145.         d->docSpec = sfr.sfFile;
  146.         SetWTitle(d->w,d->docSpec.name);
  147.         thisError = InternalSaveData(d);
  148.         }
  149.     else
  150.         thisError = 1;
  151.  
  152.     return thisError;
  153.     }
  154.  
  155.  
  156. OSErr InternalSaveData(TDoc *d)
  157.     {
  158.     TSaveRecord **srH,*sr;
  159.     short refNum;
  160.     OSErr thisError;
  161.     Rect r;
  162.  
  163.  
  164.     srH = (void *)NewHandle(sizeof(TSaveRecord));
  165.     HLock((void *)srH);
  166.     sr = *srH;
  167.  
  168.     sr->docVersion = kDocVersion;
  169.     SetPort(d->w);
  170.     sr->windowRect = qd.thePort->portRect;
  171.     LocalToGlobal((Point *)&sr->windowRect.top);
  172.     LocalToGlobal((Point *)&sr->windowRect.bottom);
  173.     sr->sr = d->sr;
  174.  
  175.     HUnlock((void *)srH);
  176.  
  177.     FSpCreateResFile(&d->docSpec,kCreatorFileType,kDocumentFileType,0);
  178.  
  179.     thisError = ResError();
  180.     if(thisError == dupFNErr)
  181.         thisError = 0;
  182.     if(thisError)
  183.         goto goHome;
  184.  
  185.     refNum = FSpOpenResFile(&d->docSpec,fsRdWrPerm); 
  186.  
  187.     if(refNum != -1)
  188.         {
  189.         Replace1Resource((void *)srH,kDocumentResType,10);
  190.         CloseResFile(refNum);
  191.         d->changed = false;
  192.         d->littleChanged = false;
  193.         d->everSaved = true;
  194.         FixUpMenus(d);
  195.         }
  196.     else
  197.         thisError = -1;
  198.  
  199. goHome:
  200.     return thisError;
  201.     }
  202.  
  203.  
  204. #define kScorePartSize (sizeof(ScorePart))
  205. #define kOldScorePartSize (sizeof(ScorePart)-4)
  206. #define kShift (4)
  207.  
  208. void UpdateSaveRecordFromEarlierVersion(TSaveRecord **sr)
  209.     {
  210.     long size;
  211.     Ptr x;
  212.     ToneDescription *td;
  213.     short i;
  214.  
  215.     size = GetHandleSize((Handle)sr);
  216.     if(size < sizeof(TSaveRecord) )
  217.         {
  218.  
  219.         SetHandleSize((Handle)sr,sizeof(TSaveRecord));
  220.         HLock((Handle)sr);
  221.         x = (Ptr)*sr;
  222.         x += sizeof(TSaveRecord);
  223.         for(i = 4; i >=1; i--)
  224.             {
  225.             x -= kScorePartSize;
  226.             BlockMove(x - kShift * i,x,kOldScorePartSize);
  227.             td = (ToneDescription *)x;
  228.             td->synthesizerType = 0;
  229.             td->synthesizerName[0] = 0;
  230.             td->instrumentName[0] = 0;
  231.             td->instrumentNumber = 0;
  232.             td->gmNumber = 1;
  233.             }
  234.         }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.     }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.